﻿//*** Guidance generated code ***//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI;

using ProjectBase.Core;
using ProjectBase.Core.Web;

namespace ProjectBase.Mvc
{
    /// <summary>
    ///     VisualControl is a base class for Controls (without ascx).
    ///     It is used as a base class for the Literal, Break, ListView...
    ///     (Literal is intended as the plain-text writer, Break represents &lt;br /&gt;)
    ///     Derived types:
    ///     1) ContentControl is intended to have children in its Control collection
    ///     2) PropertyControl serves as a base for childless (leaf) controls
    ///     used to set/get property values (e.g. Model.Item.ID)
    /// </summary>
    /// <typeparam name = "TModel"></typeparam>
    public abstract class VisualControl<TModel> : ControlHtml<TModel>, IVisualControl//: ViewUserControl<TModel>, IVisualControl
        where TModel : class, ICoreModel
    {
        #region Attributes
        protected string GetAttribute(string attributeName)
        {
            return attributeName.IsNotEmpty()
                ? Attributes[attributeName]
                : string.Empty;
        }

        protected void SetAttribute(string attributeName, object value)
        {
            if (attributeName.IsNotEmpty()
                && value.Is())
            {
                Attributes.Add(attributeName, value.ToString());
            }
        }
        #endregion Attributes

        #region HtmlString
        public virtual string ToHtmlString()
        {
            var builder = new StringBuilder();
            using (var stringWriter = new StringWriter(builder))
            {
                var writer = new HtmlTextWriter(stringWriter);
                RenderControl(writer);
            }
            return builder.ToString();
        }
        #endregion HtmlString

        #region IButtonItem -> Get Form 'Action' URL
        /// <summary>
        ///     Returns the Url for this button Form 'action'.
        ///     1) firstly test if there is filled 'button.FormActionUrl'
        ///     2) assembles the URL using UrlHelper, button.FormAction
        ///     and button or model controller
        /// </summary>
        /// <returns>URL for this button Form 'action' attribute</returns>
        public virtual string GetFormActionUrl(IButtonItem button)
        {
            var controller = button.FormController;
            if (controller.IsEmpty())
            {
                var webModel = Model as IViewModel;
                if (webModel.Is())
                {
                    controller = webModel.MasterModel.ControllerName;
                }
            }
            return Url.Action(button.FormAction, controller, button.FormRouteValues);
        }
        #endregion IButtonItem -> Get Form 'Action' URL

        #region GetLocalized (prefered way on UI)
        /// <summary>
        ///     Returns Html.ENCODED string!
        ///     Translates provided "key" into user's currently selected language.
        ///     As the parameter "area" for Transaltor is used "ControllerName"
        /// </summary>
        /// <param name = "key">Searched key</param>
        /// <returns>Transalted value (or key if not found)</returns>
        public string GetLocalized(string key)
        {
            var webModel = Model as IViewModel;
            var area = webModel.Is()
                           ? webModel.MasterModel.ControllerName
                           : string.Empty;

            return key.GetLocalized(area);
        }
        #endregion GetLocalized
    }
}